<?php
/**
 * @version      BOSSCMS-Free
 * @trademark    BOSSCMS
 * @copyright    Copyright © by BOSSCMS-Free (https://www.bosscms.net/) 保留所有权利
 * @license      https://gitee.com/Greenpeas/BOSSCMS/license/ 和 https://www.bosscms.net/freelicense/
 * @link         https://www.bosscms.net/
 * @author       温州软界网络技术有限公司
**/
defined('IS_OK') or exit('Access Forbidden');
into::basic_class('web');

class items extends web
{
	public function init()
	{
		global $G;
		$html = false;
		$G['items'] = $this->column();
		$G['tag'] = $this->tag();
		if(!isset($G['theme']['path'])){
			$G['theme']['path'] = $this->type($G['items']['type']);
		}
		$html = $this->theme($G['theme']['path']);
		/* 执行插件中的函数 */
		if($G['plugin']){
			foreach($G['plugin'] as $class){
				if($class && method_exists($class, 'over')){
					$html = $class->over($html, 'web');
				}
			}
		}
		echo $this->ad($html);
	}
	
	public function tag()
	{
		global $G;
		$tag = null;
		if($t = arrExist($G,'get|tag')){
			if($res = page::tag_name($t)){
				$tag = $res;
			}
		}
		return $tag;
	}
	
	public function column($id=0)
	{
		global $G;
		if($id){
			$items = page::items_one($id);
		}else if($G['path']['home']){
			$items = $G['home'];
		}else if(!$G['get']['items'] || is_numeric($G['get']['items'])){
			$items = page::items_one($G['get']['items'], '*', "folder='{$G['path']['folder']}'", "level ASC,id ASC");
		}
		if($items){
			$items['parents'] = /* 该栏目的父级 */
			$items['max'] = /* 最大的父级栏目id（不考虑类型和模板主题风格） */
			$items['max_parent'] = /* 最大的拥有同样类型的父级栏目id */
			$items['max_parents'] =  /* 最大的拥有同样类型且模板主题风格一样的父级栏目id */
			$items['id'];
			if($items['parent']){
				$items['parent_list'] = page::items('-'.$items['parent']);
				if($items['parent_list']){
					$items['parents'] = $items['parent'];
					$max_parent = $max_parents = true;
					foreach($items['parent_list'] as $k=>$v){
						if($max_parents && $v['type']==$items['type'] && $v['theme']==$items['theme']){
							$items['max_parents'] = $v['id'];
						}else{
							$max_parents = false;
						}
						if($max_parent && $v['type']==$items['type']){
							$items['max_parent'] = $v['id'];
						}else{
							$max_parent = false;
						}
						$items['max'] = $v['id'];
					}
				}
			}
			$items['children'] = mysql::total('items',"parent='{$items['id']}' AND display='1'");
			$data = page::config_option($items['id'],0);
			if($data){
				$G['config'] = array_merge($data, $G['config']);
			}
		}else{
			url::page404();
		}
		return $items;
	}
	
	public function type($number)
	{
		global $G;
		$table = array_search($number,$G['pass']['type']);
		$name = '';
		if(preg_match('/^(0|10)$/',$number)){
			$name = $G['items']['theme']?$G['items']['theme']:$table.'.html';
		}else if(preg_match('/^(2|3|4|5)$/',$number)){	
			if(isset($G['get']['id'])){
				if(is_numeric($G['get']['id']) && $G['group']=page::group_one($G['get']['id'],$G['items']['type'])){
					if($G['items']['id'] != $G['group']['items']){
						$items = $G['items'];
						if($G['items'] = $this->column($G['group']['items'])){
							if($G['items']['parent_list'][$items['id']] && $items['type']==$G['items']['type']){
								$G['get']['items'] = $G['group']['items'];
							}else{
								url::page404();
							}
						}
					}
				}else{
					url::page404();
				}
			}
			if(isset($G['group'])){
				if($G['group']['theme']){
					$name = $table.'_detail/'.$G['group']['theme'];
				}else{
					$name = $table.'_detail/'.($G['items']['themes']?$G['items']['themes']:$table.'_detail.html');
				}
			}else{
				$name = $table.'/'.($G['items']['theme']?$G['items']['theme']:$table.'.html');
				if($tag = arrExist($G,'get|tag')){
					$G['groups'] = page::group_tag($tag,$G['items']['type']);
				}else{
					$G['groups'] = page::group_list($G['items']['id']);
				}
			}
		}else if(preg_match('/^(1|6|7|8)$/',$number)){	
			if($number==7){
				if($G['config']['search_open']){
					$G['keyword'] = $keyword = arrExist($G,"get|{$G['config']['search_keyword']}");
					if(isset($keyword) && $keyword!==''){
						$ip = getIP();
						if(!is_numeric($G['config']['search_interval_time'])){
							$G['config']['search_interval_time'] = 5;
						}
						if(!$G['config']['search_interval_prompt']){
							$G['config']['search_interval_prompt'] = '[s]秒内只能搜索一次';
						}
						if($G['config']['search_interval_time'] && mysql::total('search',"ip='{$ip}' AND ctime>".(TIME-$G['config']['search_interval_time']))){
							alert(str_replace('[s]',$G['config']['search_interval_time'],$G['config']['search_interval_prompt']),$G['items']['url']);
						}else{
							$G['config']['search_null'] = str_replace('[keyword]','[<strong>'.stripslashes($keyword).'</strong>]',$G['config']['search_null']);
							if($G['config']['search_record']){
								if($sear = mysql::select_one('id','search',"parent='{$G['items']['id']}' AND keyword='{$keyword}' AND ip='{$ip}' AND ctime>".(TIME-3600))){
									mysql::update(array('ctime'=>TIME),'search',"id='{$sear['id']}'");
								}else{
									mysql::insert(array('parent'=>$G['items']['id'],'keyword'=>$keyword,'ip'=>$ip,'ctime'=>TIME),'search');
								}
							}
						}
					}
				}else{
					url::page404();
				}
			}
			$name = $table.'/'.($G['items']['theme']?$G['items']['theme']:$table.'.html');
		}
		return $name;
	}

	public function ad($html)
	{
		global $G;
		if(!arrExist(json::decode($G['config']['authorize']),'auth')){
			$adjs = '<script src="https://accounts.bosscms.net/advert/free/s'.(isMobile()?1:'').'.js"></script>';
			if(preg_match('/<\/body>[\s\r\n\t]+<\/html>[\s\r\n\t]*$/',$html)){
				$html = preg_replace('/<\/body>[\s\r\n\t]+<\/html>[\s\r\n\t]*$/',$adjs."\n\\0",$html);
			}else{
				$html .= $adjs;
			}
		}
		return $html;
	}
}
?>